# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userCodeDao  import UserCodeDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

@app.route('/queryUserCodeById', methods=['POST'])
def queryUserCodeByIdNoToken():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON = UserCodeDao().queryUserCodeById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserCode OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserCodeByCondition', methods=['POST'])
def queryUserCodeByConditionNoToken():

    if not request.json:
        abort(400)
    userId = request.json.get("userId")
    pageIndex = request.json.get("pageIndex",1)#传递页面索引号，默认为1

    pageSize = request.json.get("pageSize",10)

    try:
        total_count, UserCodeJSON = UserCodeDao().queryUserCodeByCondition(userId=userId,pageIndex=pageIndex,pageSize=pageSize)

        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserCode OK"},"total":total_count,"info":UserCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/insertUserCode', methods=['POST'])
def insertUserCodeNoToken():

    if not request.json:
        abort(400)

    Info = {
        'userId': request.json.get('userId'),  # 必填，请传递userd
        'userName': request.json.get('userName'),  #
        'type': request.json.get('type'),  #
        'stringCode': request.json.get('stringCode'),  #
        'count': request.json.get('count'),#
        'status':request.json.get('status'),
        'userIp': request.json.get('userIp'),  #
        'effTime': request.json.get('effTime')#
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserCodeDao().addUserCode(Info)
        # return jsonify({"message": {"code": 0, "info": "信息填写错误"}})
        if rs:
            return jsonify(
                {  "massage": {"code": 1, "info": "addUserCode OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserCode Error"}})  # 添加函数调用失败


    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/updateUserCode', methods=['POST'])
def updateUserCodeNoToken():#无需更新


    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = UserCodeDao().selectUserCode(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
        'id':request.json.get('id'),
        'userId': request.json.get('userId',oldInfo[1]),  # 必填，请传递userd
        'userName': request.json.get('userName',oldInfo[2]),  #
        'type': request.json.get('type',oldInfo[3]),  #
        'stringCode': request.json.get('stringCode',oldInfo[4]),  #
        'count': request.json.get('count',oldInfo[5]),#
        'status':request.json.get('status',oldInfo[6]),
        'userIp': request.json.get('userIp',oldInfo[7]),  #
        'effTime': request.json.get('effTime',oldInfo[8])#
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserCodeDao().updateUserCode(Info)

        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateUserCode OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateUserCode Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/deleteUserCode', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserCodeNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = UserCodeDao().deleteCode(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUserCode OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUserCode Error"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

